/**
 *
 */
package com.beta.restapi.controller;

import com.beta.common.web.ContentType;
import com.beta.entity.Categories;
import com.beta.restapi.internal.Consts;
import com.beta.restapi.internal.annotation.AuthSetting;
import com.beta.restapi.internal.annotation.AuthStrategy;
import com.beta.vo.CategoryVO;
import com.beta.restapi.response.wrapper.CategoryVoWrapper;
import com.beta.service.ICategoriesService;
import io.swagger.annotations.Api;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author vector
 */
@Api(tags = Consts.Resource.CATEGORIES)
@RestController
@RequestMapping(Consts.API_PATH_PREFIX)
public class CategoryController extends BaseController {

	@Autowired
	private ICategoriesService categoriesService;
	@Autowired
	private MapperFacade beanMapper;

	@AuthSetting(level = AuthStrategy.ANONYMOUS)
	@GetMapping(value = Consts.Resource.CATEGORIES ,produces = {ContentType.JSON})
	public Object getCategories() {
		List<Categories> categoriesList= categoriesService.getAllCategories();
		CategoryVoWrapper categoriesVO = new CategoryVoWrapper();
		categoriesVO.setCategories(beanMapper.mapAsList(categoriesList, CategoryVO.class));
		return ok(categoriesVO);
	}

	@GetMapping(value = "categories_user_signed_in", produces = {ContentType.JSON})
	public Object getSignedCategories(@RequestHeader(Consts.HEADER_AUTH_KEY)String token) {
		List<Categories> categoriesList=categoriesService.getCategories(getCurrentUserId());
		CategoryVoWrapper categoriesVO = new CategoryVoWrapper();
		categoriesVO.setCategories(beanMapper.mapAsList(categoriesList, CategoryVO.class));
		return ok(categoriesVO);
	}
}
